package com.example.clxy.controller;

import com.example.clxy.dao.AdjustscheduleMapper;
import com.example.clxy.dao.ScheduleMapper;
import com.example.clxy.dao.UserMapper;
import com.example.clxy.entity.Adjustschedule;
import com.example.clxy.entity.Schedule;
import com.example.clxy.entity.User;
import com.example.clxy.result.business.PageResult.AdjustSchedulePageResult;
import com.example.clxy.result.business.PageResult.SchedulePageResult;
import com.example.clxy.result.business.SemesterResult;
import com.example.clxy.service.*;
import com.example.clxy.util.HttpUtils;
import com.example.clxy.util.Tools;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author liuhaoqi
 * @Date 2020/8/16 18:24
 * @Version 1.0
 */
@Service
public class TaskController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    ScheduleMapper scheduleMapper;
    @Autowired
    SemesterService semesterService;
    @Autowired
    SendMailService sendMailService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AdjustscheduleMapper adjustscheduleMapper;
    @Autowired
    AdjustScheduleService adjustScheduleService;

    private static final String appid = "wxacda103c0234394b";
    private static final String secret = "86bcfa86a91fddb91985db1360ebae5d";


    @Scheduled(cron = "0 10 0/1 * * ?")
    public void updateAccessToken() {
        Map<String, String> param = new HashMap<>();
        param.put("appid", appid);
        param.put("secret", secret);
        param.put("grant_type", "client_credential");
        String result = HttpUtils.doGet("https://api.weixin.qq.com/cgi-bin/token?", param);
        System.out.println(result);
        JSONObject jsonObject = new JSONObject(result);
        if (jsonObject.has("access_token")) {
            System.out.println(jsonObject.getString("access_token"));
            redisTemplate.opsForValue().set("access_token", jsonObject.getString("access_token"));
//            userMapper.updateAccessToken(jsonObject.getString("access_token"));
        }
    }

    @Scheduled(cron = "0 0 20 ? * SUN")
    public void sendWeekEmail() {
        Date startdate = new Date();
        SemesterResult semester = semesterService.getSemesterByDate(Tools.date2Str(startdate));
        Date enddate = new Date(startdate.getTime() + (7 * 24 * 60 * 60 * 1000L));
        List<User> userList = userMapper.getTeacherList();
        for (User user : userList) {
            String tcemail = user.getTcemail();
            String teachername = user.getUsername();
            String userid = user.getUserid();
            List<SchedulePageResult> schedulePageResults = scheduleMapper.getScheduleListByWeekTeacher(userid, 1, 10000,
                    10000, Tools.date2Str(startdate), Tools.date2Str(enddate));
            if (schedulePageResults.size() > 0) {
                String experimentstr = "";
                for (SchedulePageResult schedulePageResult : schedulePageResults) {
                    experimentstr += schedulePageResult.getExperimentname() + "实验，时间为：" + schedulePageResult.getStartdate()
                            + "~" + Tools.date2Str(Tools.str2Date(schedulePageResult.getEnddate()), "HH:mm:ss") + "\n";
                }
                String content = "尊敬的" + teachername + "老师,在下周你有\n";

                String content2 = "\n实验需要上，切勿忘记哦！！" + "\n(本邮件来自湖南大学材料学院管理后台系统)";
                sendMailService.sendSimpleMail(tcemail, "材料学院管理平台", content + experimentstr + content2);
            }
        }
    }

    @Scheduled(cron = "0 0 0 * * ?")
    public void handleAdjustSchedule() {
        List<AdjustSchedulePageResult> adjustSchedulePageResults = adjustscheduleMapper.getAdjustSchedule(null, null, "0", 0, 10000, 10000);
        for (AdjustSchedulePageResult adjustSchedulePageResult : adjustSchedulePageResults) {
            Schedule schedule = scheduleMapper.selectByPrimaryKey(adjustSchedulePageResult.getNewscheduleid());
            Date date = new Date();
            if (date.getTime() > schedule.getEnddate().getTime()) {
                Adjustschedule adjustschedule = new Adjustschedule();
                adjustschedule.setAdjustclassid(adjustSchedulePageResult.getAdjustclassid());
                adjustschedule.setAdjuststate("2");
                adjustschedule.setSpreason("申请调到的另外一门课程其课程结束时间已过，此调课申请已无效，系统自动拒绝此调课申请");
                adjustScheduleService.updateAdjustSchedule(adjustschedule);
            }
        }
    }
}
